home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gas_251.zip / bin_251 / ld / ldmisc.c < prev    next >
C/C++ Source or Header  |  1994-09-15  |  9KB  |  436 lines

  1. /* ldmisc.c
  2.    Copyright (C) 1991, 92, 93, 94 Free Software Foundation, Inc.
  3.  
  4.    Written by Steve Chamberlain of Cygnus Support.
  5.  
  6. This file is part of GLD, the Gnu Linker.
  7.  
  8. GLD is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2, or (at your option)
  11. any later version.
  12.  
  13. GLD is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with GLD; see the file COPYING.  If not, write to
  20. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. #include "bfd.h"
  23. #include "sysdep.h"
  24. #include <varargs.h>
  25. #include <demangle.h>
  26.  
  27. #include "ld.h"
  28. #include "ldmisc.h"
  29. #include "ldexp.h"
  30. #include "ldlang.h"
  31. #include "ldgram.h"
  32. #include "ldlex.h"
  33. #include "ldmain.h"
  34. #include "ldfile.h"
  35.  
  36. /* VARARGS*/
  37. static void finfo ();
  38. static const char *demangle PARAMS ((const char *string,
  39.                      int remove_underscore));
  40.  
  41. /*
  42.  %% literal %
  43.  %F error is fatal
  44.  %P print program name
  45.  %S print script file and linenumber
  46.  %E current bfd error or errno
  47.  %I filename from a lang_input_statement_type
  48.  %B filename from a bfd
  49.  %T symbol name
  50.  %X no object output, fail return
  51.  %V hex bfd_vma
  52.  %v hex bfd_vma, no leading zeros
  53.  %C clever filename:linenumber with function
  54.  %D like %C, but no function name
  55.  %R info about a relent
  56.  %s arbitrary string, like printf
  57.  %d integer, like printf
  58.  %u integer, like printf
  59. */
  60.  
  61. static const char *
  62. demangle (string, remove_underscore)
  63.      const char *string;
  64.      int remove_underscore;
  65. {
  66.   const char *res;
  67.   if (remove_underscore && output_bfd) 
  68.   {
  69.     if (bfd_get_symbol_leading_char (output_bfd) == string[0])
  70.      string++;
  71.   }
  72.   /* Note that there's a memory leak here, we keep buying memory
  73.      for demangled names, and never free.  But if you have so many
  74.      errors that you run out of VM with the error messages, then
  75.      there's something up */
  76.   res = cplus_demangle (string, DMGL_ANSI|DMGL_PARAMS);
  77.   return res ? res : string;
  78. }
  79.  
  80. static void
  81. vfinfo(fp, fmt, arg)
  82.      FILE *fp;
  83.      char *fmt;
  84.      va_list arg;
  85. {
  86.   boolean fatal = false;
  87.  
  88.   while (*fmt) 
  89.   {
  90.     while (*fmt != '%' && *fmt != '\0') 
  91.     {
  92.       putc(*fmt, fp);
  93.       fmt++;
  94.     }
  95.  
  96.     if (*fmt == '%') 
  97.     {
  98.       fmt ++;
  99.       switch (*fmt++) 
  100.       {
  101.       default:
  102.     fprintf(fp,"%%%c", fmt[-1]);
  103.     break;
  104.  
  105.       case '%':
  106.     /* literal % */
  107.     putc('%', fp);
  108.     break;
  109.  
  110.        case 'X':
  111.     /* no object output, fail return */
  112.     config.make_executable = false;
  113.     break;
  114.  
  115.        case 'V':
  116.     /* hex bfd_vma */
  117.     {
  118.       bfd_vma value = va_arg(arg, bfd_vma);
  119.       fprintf_vma(fp, value);
  120.     }
  121.     break;
  122.  
  123.       case 'v':
  124.     /* hex bfd_vma, no leading zeros */
  125.     {
  126.       char buf[100];
  127.       char *p = buf;
  128.       bfd_vma value = va_arg (arg, bfd_vma);
  129.       sprintf_vma (p, value);
  130.       while (*p == '0')
  131.         p++;
  132.       if (!*p)
  133.         p--;
  134.       fputs (p, fp);
  135.     }
  136.     break;
  137.  
  138.        case 'T':
  139.     /* Symbol name.  */
  140.     {
  141.       const char *name = va_arg (arg, const char *);
  142.  
  143.       if (name != (const char *) NULL)
  144.         fprintf (fp, "%s", demangle (name, 1));
  145.       else
  146.         fprintf (fp, "no symbol");
  147.     }
  148.     break;
  149.  
  150.        case 'B':
  151.     /* filename from a bfd */
  152.        { 
  153.      bfd *abfd = va_arg(arg, bfd *);
  154.      if (abfd->my_archive) {
  155.        fprintf(fp,"%s(%s)", abfd->my_archive->filename,
  156.            abfd->filename);
  157.      }
  158.      else {
  159.        fprintf(fp,"%s", abfd->filename);
  160.      }
  161.        }
  162.     break;
  163.  
  164.        case 'F':
  165.     /* error is fatal */
  166.     fatal = true;
  167.     break;
  168.  
  169.        case 'P':
  170.     /* print program name */
  171.     fprintf(fp,"%s", program_name);
  172.     break;
  173.  
  174.        case 'E':
  175.     /* current bfd error or errno */
  176.     fprintf(fp, bfd_errmsg(bfd_get_error ()));
  177.     break;
  178.  
  179.        case 'I':
  180.     /* filename from a lang_input_statement_type */
  181.        {
  182.      lang_input_statement_type *i =
  183.       va_arg(arg,lang_input_statement_type *);
  184.  
  185.      if (i->the_bfd->my_archive)
  186.        fprintf(fp, "(%s)", i->the_bfd->my_archive->filename);
  187.      fprintf(fp,"%s", i->local_sym_name);
  188.        }
  189.     break;
  190.  
  191.        case 'S':
  192.     /* print script file and linenumber */
  193.        {
  194.      if (ldfile_input_filename) {
  195.        fprintf(fp,"%s:%u", ldfile_input_filename, lineno );
  196.      }
  197.        }
  198.     break;
  199.  
  200.        case 'R':
  201.     /* Print all that's interesting about a relent */
  202.        {
  203.      arelent *relent = va_arg(arg, arelent *);
  204.     
  205.      finfo (fp, "%s+0x%v (type %s)",
  206.         (*(relent->sym_ptr_ptr))->name,
  207.         relent->addend,
  208.         relent->howto->name);
  209.        }
  210.     break;
  211.     
  212.        case 'C':
  213.        case 'D':
  214.     /* Clever filename:linenumber with function name if possible,
  215.        or section name as a last resort.  The arguments are a BFD,
  216.        a section, and an offset.  */
  217.     {
  218.       static bfd *last_bfd;
  219.       static char *last_file = NULL;
  220.       static char *last_function = NULL;
  221.       bfd *abfd;
  222.       asection *section;
  223.       bfd_vma offset;
  224.       lang_input_statement_type *entry;
  225.       asymbol **asymbols;
  226.       const char *filename;
  227.       const char *functionname;
  228.       unsigned int linenumber;
  229.       boolean discard_last;
  230.  
  231.       abfd = va_arg (arg, bfd *);
  232.       section = va_arg (arg, asection *);
  233.       offset = va_arg (arg, bfd_vma);
  234.  
  235.       entry = (lang_input_statement_type *) abfd->usrdata;
  236.       if (entry != (lang_input_statement_type *) NULL
  237.           && entry->asymbols != (asymbol **) NULL)
  238.         asymbols = entry->asymbols;
  239.       else
  240.         {
  241.           long symsize;
  242.           long symbol_count;
  243.  
  244.           symsize = bfd_get_symtab_upper_bound (abfd);
  245.           if (symsize < 0)
  246.         einfo ("%B%F: could not read symbols", abfd);
  247.           asymbols = (asymbol **) xmalloc (symsize);
  248.           symbol_count = bfd_canonicalize_symtab (abfd, asymbols);
  249.           if (symbol_count < 0)
  250.         einfo ("%B%F: could not read symbols", abfd);
  251.           if (entry != (lang_input_statement_type *) NULL)
  252.         {
  253.           entry->asymbols = asymbols;
  254.           entry->symbol_count = symbol_count;
  255.         }
  256.         }
  257.  
  258.       discard_last = true;
  259.       if (bfd_find_nearest_line (abfd, section, asymbols, offset,
  260.                      &filename, &functionname, &linenumber))
  261.         {
  262.           if (filename == (char *) NULL)
  263.         filename = abfd->filename;
  264.  
  265.           if (functionname != NULL && fmt[-1] == 'C')
  266.         {
  267.           if (last_bfd == NULL
  268.               || last_file == NULL
  269.               || last_function == NULL
  270.               || last_bfd != abfd
  271.               || strcmp (last_file, filename) != 0
  272.               || strcmp (last_function, functionname) != 0)
  273.             {
  274.               /* We use abfd->filename in this initial line,
  275.                          in case filename is a .h file or something
  276.                          similarly unhelpful.  */
  277.               finfo (fp, "%B: In function `%s':\n",
  278.                  abfd, demangle (functionname, 1));
  279.  
  280.               last_bfd = abfd;
  281.               if (last_file != NULL)
  282.             free (last_file);
  283.               last_file = buystring (filename);
  284.               if (last_function != NULL)
  285.             free (last_function);
  286.               last_function = buystring (functionname);
  287.             }
  288.           discard_last = false;
  289.           fprintf (fp, "%s:%u", filename, linenumber);
  290.         }
  291.           else if (linenumber != 0) 
  292.         finfo (fp, "%B:%s:%u", abfd, filename, linenumber);
  293.           else
  294.         finfo (fp, "%s(%s+0x%v)", filename, section->name, offset);
  295.         }
  296.       else
  297.         finfo (fp, "%s(%s+0x%v)", abfd->filename, section->name, offset);
  298.  
  299.       if (discard_last)
  300.         {
  301.           last_bfd = NULL;
  302.           if (last_file != NULL)
  303.         {
  304.           free (last_file);
  305.           last_file = NULL;
  306.         }
  307.           if (last_function != NULL)
  308.         {
  309.           free (last_function);
  310.           last_function = NULL;
  311.         }
  312.         }
  313.     }
  314.     break;
  315.         
  316.        case 's':
  317.     /* arbitrary string, like printf */
  318.     fprintf(fp,"%s", va_arg(arg, char *));
  319.     break;
  320.  
  321.        case 'd':
  322.     /* integer, like printf */
  323.     fprintf(fp,"%d", va_arg(arg, int));
  324.     break;
  325.  
  326.        case 'u':
  327.     /* unsigned integer, like printf */
  328.     fprintf(fp,"%u", va_arg(arg, unsigned int));
  329.     break;
  330.       }
  331.     }
  332.   }
  333.  
  334.   if (fatal == true) 
  335.     xexit(1);
  336. }
  337.  
  338. /* Format info message and print on stdout. */
  339.  
  340. /* (You would think this should be called just "info", but then you would
  341.    hosed by LynxOS, which defines that name in its libc.) */
  342.  
  343. void info_msg(va_alist)
  344.      va_dcl
  345. {
  346.   char *fmt;
  347.   va_list arg;
  348.   va_start(arg);
  349.   fmt = va_arg(arg, char *);
  350.   vfinfo(stdout, fmt, arg);
  351.   va_end(arg);
  352. }
  353.  
  354. /* ('e' for error.) Format info message and print on stderr. */
  355.  
  356. void einfo(va_alist)
  357.      va_dcl
  358. {
  359.   char *fmt;
  360.   va_list arg;
  361.   va_start(arg);
  362.   fmt = va_arg(arg, char *);
  363.   vfinfo(stderr, fmt, arg);
  364.   va_end(arg);
  365. }
  366.  
  367. void 
  368. info_assert(file, line)
  369.      char *file;
  370.      unsigned int line;
  371. {
  372.   einfo("%F%P: internal error %s %d\n", file,line);
  373. }
  374.  
  375. char *
  376. buystring (x)
  377.      CONST char *CONST x;
  378. {
  379.   size_t l = strlen(x)+1;
  380.   char *r = xmalloc(l);
  381.   memcpy(r, x,l);
  382.   return r;
  383. }
  384.  
  385.  
  386. /* ('m' for map) Format info message and print on map. */
  387.  
  388. void minfo(va_alist)
  389.      va_dcl
  390. {
  391.   char *fmt;
  392.   va_list arg;
  393.   va_start(arg);
  394.   fmt = va_arg(arg, char *);
  395.   vfinfo(config.map_file, fmt, arg);
  396.   va_end(arg);
  397. }
  398.  
  399.  
  400. static void
  401. finfo (va_alist)
  402.      va_dcl
  403. {
  404.   char *fmt;
  405.   FILE *file;
  406.   va_list arg;
  407.   va_start (arg);
  408.   file = va_arg (arg, FILE *);
  409.   fmt = va_arg (arg, char *);
  410.   vfinfo (file, fmt, arg);
  411.   va_end (arg);
  412. }
  413.  
  414.  
  415.  
  416. /*----------------------------------------------------------------------
  417.   Functions to print the link map 
  418.  */
  419.  
  420. void 
  421. print_space ()
  422. {
  423.   fprintf(config.map_file, " ");
  424. }
  425. void 
  426. print_nl ()
  427. {
  428.   fprintf(config.map_file, "\n");
  429. }
  430. void 
  431. print_address (value)
  432.      bfd_vma value;
  433. {
  434.   fprintf_vma(config.map_file, value);
  435. }
  436.